package org.csu.molediary.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.molediary.api.common.CommonResponse;
import org.csu.molediary.api.entity.Account;
import org.csu.molediary.api.entity.Diary;
import org.csu.molediary.api.persistence.AccountMapper;
import org.csu.molediary.api.persistence.DiaryMapper;
import org.csu.molediary.api.service.AdminDiaryService;
import org.csu.molediary.api.vo.AdminDiaryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service("AdminDiaryService")
public class AdminDiaryServiceImpl implements AdminDiaryService {

    @Autowired
    private DiaryMapper diaryMapper;

    @Autowired
    private AccountMapper accountMapper;

    @Override
    //管理员获得所有日记
    public CommonResponse<List<AdminDiaryVO>> adminGetAllDiary() {
        List<Diary> diaryList = diaryMapper.selectList(null);
        List<AdminDiaryVO> adminDiaryVOList = new ArrayList<>();

        //为adminDiaryVOList插入Diary对象
        Iterator<Diary> diaryIterator = diaryList.iterator();
        while(diaryIterator.hasNext()) {
            Diary diary = diaryIterator.next();
            AdminDiaryVO adminDiaryVO = new AdminDiaryVO();

            adminDiaryVO.setAccountid(diary.getAccountid());
            adminDiaryVO.setCity(diary.getCity());
            adminDiaryVO.setContent(diary.getContent());
            adminDiaryVO.setContentid(diary.getContentid());
            adminDiaryVO.setDiarydate(diary.getDiarydate());
            adminDiaryVO.setDistinct(diary.getDistinct1());
            adminDiaryVO.setGoodnum(diary.getGoodnum());
            adminDiaryVO.setProvince(diary.getProvince());
            adminDiaryVO.setTagid(diary.getTagid());
            adminDiaryVO.setTitle(diary.getTitle());

            QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
            accountQueryWrapper.eq("id",diary.getAccountid());
            Account account = accountMapper.selectOne(accountQueryWrapper);

            adminDiaryVO.setName(account.getName());

            adminDiaryVOList.add(adminDiaryVO);
        }
        if(adminDiaryVOList.isEmpty()){
            return CommonResponse.createForAdminSuccessMessage("没有日记");
        }

        return CommonResponse.createForAdminSuccess("",adminDiaryVOList);
    }

    @Override
    //管理员通过用户id查找日记
    public CommonResponse<List<AdminDiaryVO>> adminGetDiaryByAccountId(String accountid) {
        QueryWrapper<Diary> diaryQueryWrapper = new QueryWrapper<>();
        diaryQueryWrapper.eq("accountid",accountid);
        List<Diary> diaryList = diaryMapper.selectList(diaryQueryWrapper);
        List<AdminDiaryVO> adminDiaryVOList = new ArrayList<>();

        //为adminDiaryVOList插入Diary对象
        Iterator<Diary> diaryIterator = diaryList.iterator();
        while(diaryIterator.hasNext()) {
            Diary diary = diaryIterator.next();
            AdminDiaryVO adminDiaryVO = new AdminDiaryVO();

            adminDiaryVO.setAccountid(diary.getAccountid());
            adminDiaryVO.setCity(diary.getCity());
            adminDiaryVO.setContent(diary.getContent());
            adminDiaryVO.setContentid(diary.getContentid());
            adminDiaryVO.setDiarydate(diary.getDiarydate());
            adminDiaryVO.setDistinct(diary.getDistinct1());
            adminDiaryVO.setGoodnum(diary.getGoodnum());
            adminDiaryVO.setProvince(diary.getProvince());
            adminDiaryVO.setTagid(diary.getTagid());
            adminDiaryVO.setTitle(diary.getTitle());

            QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
            accountQueryWrapper.eq("id",diary.getAccountid());
            Account account = accountMapper.selectOne(accountQueryWrapper);

            adminDiaryVO.setName(account.getName());

            adminDiaryVOList.add(adminDiaryVO);
        }
        if(adminDiaryVOList.isEmpty()){
            return CommonResponse.createForAdminSuccessMessage("没有日记");
        }
        return CommonResponse.createForAdminSuccess("",adminDiaryVOList);
    }

    @Override
    //管理员删除日记
    public CommonResponse<String> adminDeleteDiary(int contentid) {
        QueryWrapper<Diary> diaryQueryWrapper = new QueryWrapper<>();
        diaryQueryWrapper.eq("contentid",contentid);
        Diary diary = diaryMapper.selectOne(diaryQueryWrapper);
        if(diary==null){
            return CommonResponse.createForAdminError("没有对应该id的日记，故删除无效");
        }
        diaryMapper.delete(diaryQueryWrapper);
        return CommonResponse.createForAdminSuccessMessage("删除成功");
    }
}
